package com.hades.xq.springcool.business.controller

import com.hades.xq.springcool.business.dao.entity.UserEntity
import com.hades.xq.springcool.business.repository.UserRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest

@RestController
@RequestMapping(value = "/user")
class LoginController {

    @Autowired
    private lateinit var userJpa: UserRepository


    @RequestMapping(value = "/login")//method = [RequestMethod.POST]
    fun login(userName: String?, userPwd: String?, request: HttpServletRequest): String {
        println("userName = [$userName], userPwd = [$userPwd]")
        val userBean = userJpa.findById(1L).get()
        return if (userBean.userName == userName && userBean.password == userPwd) {
            request.session.setAttribute("_session_user", userBean)
            "登录成功"
        } else {
            "用户名或密码错误"
        }
    }


    private fun getUser() = UserEntity().apply {
        this.userName = "cool"
        this.password = "123456"
        this.age = 20
        this.address = "武汉"
        this.createTime = System.currentTimeMillis()
    }
}